<template>
  <div class="edit-popup">
    <popup
        ref="popupRef"
        :title="popupTitle"
        :async="true"
        width="550px"
        :clickModalClose="true"
        @confirm="handleSubmit"
        @close="handleClose"
    >
      <el-form ref="formRef" :model="formData" label-width="200px" :rules="formRules" label-position="top">
        <el-form-item label="城市" prop="name">
          <el-select v-model="formData.areaId" class="w-[280px]" placeholder="请选择城市" @change="cityChange">
            <el-option v-for="(item, index) in optionsData.city" :key="index" :label="item.areaName" :value="item.areaId"/>
          </el-select>
        </el-form-item>
        <el-form-item label="联系电话" prop="mobile">
          <el-input v-model="formData.mobile" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="受委托方名称" prop="commissionedParty">
          <el-input v-model="formData.commissionedParty" placeholder="请输入受委托方" />
        </el-form-item>
        <el-form-item label="住所地" prop="address">
          <el-input
              v-model="formData.address"
              placeholder="请输入住所地"
              type="textarea"
              :autosize="{ minRows: 4, maxRows: 6 }"
          />
        </el-form-item>
        <el-form-item label="身份证号码/组织机构代码证" prop="certificateNo">
          <el-input v-model="formData.certificateNo" placeholder="请输入身份证号码/组织机构代码证" />
        </el-form-item>
        <el-form-item label="银行户名" prop="accountName">
          <el-input v-model="formData.accountName" placeholder="请输入户名" />
        </el-form-item>
        <el-form-item label="银行账号" prop="accountNumber">
          <el-input v-model="formData.accountNumber" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item label="开户行" prop="bankName">
          <el-input v-model="formData.bankName" placeholder="请输入开户行" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="formData.status" placeholder="请选择1-启用">
            <!--            <el-radio label="0">请选择字典生成</el-radio>-->
            <el-radio
                v-for="(item, index) in dictData.status"
                :key="index"
                :label="parseInt(item.value)"
                :disabled="!item.status"
            >
              {{ item.name }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序（倒序）" prop="sort">
          <el-input-number v-model="formData.sort" :max="9999" />
        </el-form-item>
      </el-form>
    </popup>
  </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import {  areasEdit, areasAdd, areasDetail } from '@/api/areas'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
import {useDictOptions} from "@/hooks/useDictOptions";
import {allCity, allProvince} from "@/api/city";
defineProps({
  dictData: {
    type: Object as PropType<Record<string, any[]>>,
    default: () => ({})
  }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
  return mode.value == 'edit' ? '编辑地区' : '新增地区'
})

const formData = reactive({
  id: '',
  name: '',
  areaId: '',
  mobile: '',
  commissionedParty: '',
  address: '',
  certificateNo: '',
  accountName: '',
  accountNumber: '',
  bankName: '',
  status: 1,
  sort: 0,
})

const { optionsData } = useDictOptions<{
  city: any[]
}>({
  city: {
    api: allCity
  }
})

const formRules = {
  name: [
    {
      required: true,
      message: '请选择城市',
      trigger: ['blur']
    }
  ],
  mobile: [
    {
      required: true,
      message: '请输入手机号',
      trigger: ['blur']
    }
  ],
  commissionedParty: [
    {
      required: true,
      message: '请输入受委托方',
      trigger: ['blur']
    }
  ],
  address: [
    {
      required: true,
      message: '请输入住所地',
      trigger: ['blur']
    }
  ],
  certificateNo: [
    {
      required: true,
      message: '请输入身份证号码/组织机构代码证',
      trigger: ['blur']
    }
  ],
  accountName: [
    {
      required: true,
      message: '请输入户名',
      trigger: ['blur']
    }
  ],
  accountNumber: [
    {
      required: true,
      message: '请输入账号',
      trigger: ['blur']
    }
  ],
  bankName: [
    {
      required: true,
      message: '请输入开户行',
      trigger: ['blur']
    }
  ],
  status: [
    {
      required: true,
      message: '请选择1-启用',
      trigger: ['blur']
    }
  ],
  sort: [
    {
      required: true,
      message: '请输入序号',
      trigger: ['blur']
    }
  ],
  areaId: [
    {
      required: true,
      message: '请选择城市',
      trigger: ['blur']
    }
  ],
}

const cityChange = (areaId) => {
  let area = optionsData.city.filter(item=> item.areaId === areaId)
  formData.name = area[0].areaName
}

const handleSubmit = async () => {
  await formRef.value?.validate()
  const data: any = { ...formData }
  mode.value == 'edit' ? await areasEdit(data) : await areasAdd(data)
  popupRef.value?.close()
  feedback.msgSuccess('操作成功')
  emit('success')
}

const open = (type = 'add') => {
  mode.value = type
  popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      //@ts-ignore
      formData[key] = data[key]
    }
  }
}

const getDetail = async (row: Record<string, any>) => {
  const data = await areasDetail({
    id: row.id
  })
  setFormData(data)
}

const handleClose = () => {
  emit('close')
}

defineExpose({
  open,
  setFormData,
  getDetail
})
</script>
